﻿using AutoMapper;
using BasicData.Domain.Entities;
using BasicData.Infrastructure.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Warehouse.Application.Dto.Input;
using Warehouse.Application.Dto.Output;
using Warehouse.Domain.Entities;
using Warehouse.Infrastructure.Repositories;
using Warehouse.Result;
using Yitter.IdGenerator;

namespace Warehouse.Application.Services.SuppliedMaterials
{
    public class SuppliedMaterialsAppService:ISuppliedMaterialsAppService
    {
        private readonly IWarehouseModelRepository<SuppliedMaterialsModel> _suppliedMaterialsModelRepository;
        private readonly IMapper _mapper;
        private readonly IBasicModelRepository<SupplierModel> _supplierModelRepository;
        private readonly IBasicModelRepository<ProductModel> _productModelRepository;

        public SuppliedMaterialsAppService(IWarehouseModelRepository<SuppliedMaterialsModel> suppliedMaterialsModelRepository, IMapper mapper, IBasicModelRepository<SupplierModel> supplierModelRepository, IBasicModelRepository<ProductModel> productModelRepository)
        {
            _suppliedMaterialsModelRepository = suppliedMaterialsModelRepository;
            _mapper = mapper;
            _supplierModelRepository = supplierModelRepository;
            _productModelRepository = productModelRepository;
        }


        /// <summary>
        /// 来料标签打印
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> CreateSuppliedMaterials(CreateSuppliedMaterialsInput input)
        {
            var model = _mapper.Map<SuppliedMaterialsModel>(input);
            model.Id = YitIdHelper.NextId();
            model.CreatedAt = DateTime.Now;
            model.IsDeleted = false;
            // 获取当前日期并格式化为yyyyMMdd
            string dateStr = DateTime.Now.ToString("yyyyMMdd");

            // 查询当天已有的记录数
            string prefix = "MT" + dateStr;
            var todayItems = _suppliedMaterialsModelRepository.GetValue()
                .Where(x => x.Rid != null &&
                            x.Rid.StartsWith(prefix) &&
                            x.CreatedAt.Date == DateTime.Now.Date)
                .ToList();

            // 计算序号
            int sequence = todayItems.Count + 1;

            // 生成编码
            string coding = $"{prefix}{sequence:D4}";
            model.Rid = coding;


            _suppliedMaterialsModelRepository.Create(model);
            return new ApiResult<int>()
            {
                Code = ApiEnum.Ok,
                Message = "添加来料标签成功",
                Data = 1
            };
        }


        /// <summary>
        /// 获取来料标签
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiPaing<ListSuppliedMaterialsOutput> GetSuppliedMaterials(PageInput input)
        {
            var SuppliedMaterials = _suppliedMaterialsModelRepository.GetValue().Where(x => x.IsDeleted == false);
            var Supplied = _supplierModelRepository.GetValue().Where(x => x.IsDeleted == false);
            var material = _productModelRepository.GetValue().Where(x => x.IsDeleted == false);

            var linq = from x in SuppliedMaterials
                       join
                      y in Supplied on x.Suppilerid equals y.Id
                       join
                       z in material on x.MaterialId equals z.Id
                       select new ListSuppliedMaterialsOutput()
                       {
                           Id = x.Id,
                           Source = x.Source,
                           DeliveryNumber = x.DeliveryNumber,
                           Suppilerid = x.Suppilerid,
                           SuppilerName = y.SupplierName,
                           Rid = x.Rid,
                           ExternalName = x.ExternalName,
                           MaterialId = x.MaterialId,
                           MaterialName = z.ProductName,
                           BatchNumber = x.BatchNumber,
                           ProductionTime = x.ProductionTime,
                           LoseefficacyTime = x.LoseefficacyTime,
                           EachNumber = x.EachNumber,
                           PrintNumber = x.PrintNumber,
                           Specification=z.Specification,
                           Unit=z.Unit,
                           ProductCode = z.ProductCode,
                       };
            var model = linq.OrderByDescending(x => x.Id).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();

            return new ApiPaing<ListSuppliedMaterialsOutput>
            {
                TotalCount = linq.Count(),
                PageDatas = model,
                Code = ApiEnum.Ok,
                Message = "成功"
            };

        }






    }
}
